﻿#-*- coding: utf-8 -*-

# modulo de operações de leitura e escrita sobre um ficheiro
# de base de dados sqlite3
# 
# IPBeja - Escola Superior de Tecnologia e Getão
# Engenharia Informática
# Elísio Gonçalves Ribeiro nº4708

#http://matplotlib.org/api/index.html

import matplotlib.pyplot as plt
import numpy as np
import string


#recebe os parametros para desenhar o(s) grafico(s), e 
#guardar o ficheiro .png gerado no endereco indicado
#q_data - tuplo com os labels e as listas dos valores xx e yy
#title, color - titulo e cor para o grafico
#total - dados para o grafico-por-defeito
#sv_file -onde guradar o ficheiro gerado
#
def draw_graphic(q_data, title, color, total, sv_file):
    """
    process the passed params to plot the requested graphic(s)
    over a default one and save it on disk
    q_data  - tuple with values of labels, xx and yy list
    title   - a title for the generated graphic
    color   - a specified color for the requested graphic bars
    total   - list of values for the default graphic bars
    sv_file - a name on witch to save the file
    """
    label_x = q_data[0][0].decode('utf-8')                                     #getting xx label
    label_y = q_data[0][1].decode('utf-8')                                     #getting yy label
    lst_xx = q_data[1]                                                         #getting xx value's list
    lst_yy = q_data[2]                                                         #getting yy value's list
    t_label = total[0][-1].decode('utf-8')
    
    t_label = string.replace(t_label, '_', ' ')                                #formatting
    label_x = string.replace(label_x, '_', ' ')                                #labels
    label_y = string.replace(label_y, '_', ' ')                                #names
    
    temp = []
    total = total[1]                                                           #getting 
    [temp.append(t[1]) for i in range(len(lst_xx))                             #and processing 
    for t in total if lst_xx[i] == t[0]]                                       #the yy list for the default graphic bars
    
    fig = plt.figure(figsize=(35,8))
    ax = fig.add_subplot(111)
    xtval = []                                                                 #list of ploted graphics ticks
    xv=np.arange(len(lst_xx))
    xtval.append( ax.bar(xv, temp, color='k'))                                 #plotting the default graphic bars
    xtval.append( ax.bar(xv, lst_yy, color=color))                             #plotting the requested graphic bars

    ax.set_ylabel(label_y)
    ax.set_xlabel(label_x)
    ax.set_title(title)
    ax.set_xticks(xv)                                                          #setting position for values for xx axes 
    ax.set_xticklabels((lst_xx ), rotation='vertical', 
                                color='k', ha='center',size='small')           #setting values on xx axes
    
    for x,y in zip(xv,lst_yy):
        ax.text(x+0.4, y+.06, '%.2f' % y, ha='center', color='c', 
                    size='x-small',rotation='vertical', va= 'bottom')          #setting values on top of the requested graphic bars
    ax.grid()
    ax.legend( xtval, (t_label,label_y), loc='best')
    fn = 'data/'+sv_file+'_graph.png'
    fig.savefig(fn,DPI=800)
    return fn
pass
